package com.alan.camunda.boot.service;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.BpmnError;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateVariableMapping;
import org.camunda.bpm.engine.delegate.VariableScope;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.springframework.stereotype.Component;

@Slf4j
@Component("mdvm")
public class MyDelegateVariableMapping implements DelegateVariableMapping {

    @Override
    public void mapInputVariables(DelegateExecution superExecution, VariableMap subVariables) {
        String parentNodeId = superExecution.getCurrentActivityId();
        System.out.println("当前在父流程上的节点为: " + parentNodeId);

        FlowElement flowElement = superExecution.getBpmnModelElementInstance();
        

        subVariables.put("aaa", "888");
        subVariables.put("bbb", "999");

        log.info("call process mapping to child");
    }

    @Override
    public void mapOutputVariables(DelegateExecution superExecution, VariableScope subInstance) {
        // 父流程上的 CallProcess 节点的环节 ID
        String activityId = superExecution.getCurrentActivityId();
        log.info("call process mapping to parent, 当前在父流程上的节点为: {}", activityId);
        superExecution.setVariable("_R", 1);

        Object fault = subInstance.getVariable("_failed_");
        if (fault != null) {
            // 子流程 Fault
        }

        Object abort = subInstance.getVariable("_abort_");
        if (abort != null) {
            // 子流程 Abort
        }

    }

}
